/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Editor_MainGUI;

import GUI.Components.BButton;
import GUI.Components.BToggleButton;
import General.CurrentData;
import com.jme3.math.Vector3f;
import other.Wizard;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.concurrent.Callable;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

/**
 *
 * @author David
 */
public class ControlToolBar extends JToolBar
{

    /*Controls*/
    private JCheckBox gridCheckBox = new JCheckBox("Show Grid");
    private JCheckBox filterCheckBox = new JCheckBox("Enable Filters");
    private JComboBox<String> camSpeedComboBox = new JComboBox<String>(new String[]
    {
        CurrentData.CAM_VERY_SLOW + "  (Very Slow)",
        CurrentData.CAM_SLOW + "  (Slow)",
        CurrentData.CAM_DEFAULT + " (Default)",
        CurrentData.CAM_FAST + " (Fast)",
        CurrentData.CAM_VERY_FAST + " (Very Fast)",
        "Custom"
    });
    private JCheckBox wireCheckBox = new JCheckBox("Show Wire");
    private BToggleButton physicsStartStopToggleButton = new BToggleButton("Start Physics", false);
    private BButton camToCenterButton = new BButton("Center Camera");
    private JCheckBox sceneryViewCheckBox = new JCheckBox("Scenery View");
    private JCheckBox physicsDebugCheckBox = new JCheckBox("Physics Shapes");
    /*Labels*/
    private JLabel camLocationLabel = new JLabel("Camera Location: ");
    private JLabel camDirectionLabel = new JLabel("Camera Direction: ");
    private JLabel selectedObjectLabel = new JLabel("Selected Object: ");
    private JLabel dimensionLabel = new JLabel("Resolution: ");

    /**
     *
     */
    public ControlToolBar()
    {
        camSpeedComboBox.setLightWeightPopupEnabled(false);
        initConfigValues();
        initConfigListener();
        setBorderPainted(false);
        setBorder(null);
        setLayout(new GridLayout(4, 3));
        setMargin(new Insets(0, 0, 0, 0));
        add(gridCheckBox);
        add(camSpeedComboBox);
        add(selectedObjectLabel);
        add(wireCheckBox);
        add(physicsDebugCheckBox);
        add(camLocationLabel);
        add(filterCheckBox);
        add(physicsStartStopToggleButton);
        add(camDirectionLabel);
        add(sceneryViewCheckBox);
        add(camToCenterButton);
        add(dimensionLabel);
        setEnabled(false);
        new Thread(new Runnable()
        {
            public void run()
            {
                while (true)
                {
                    try
                    {
                        Thread.sleep(40);
                    } catch (InterruptedException ex)
                    {
                        CurrentData.getObserverDialog().printError("Thread.sleep in ControlToolBar interrupted", ex);
                    }
                    if (CurrentData.isAppRunning())
                    {
                        updateLabels();
                    }
                }
            }
        }).start();
    }

    private void updateLabels()
    {
        camDirectionLabel.setText("Camera Direction: " + CurrentData.getEditorWindow().getB3DApplication().getCamera().getDirection());
        camLocationLabel.setText("Camera Location: " + CurrentData.getEditorWindow().getB3DApplication().getCamera().getLocation());
        try
        {
            selectedObjectLabel.setText("ID: " + CurrentData.getEditorWindow().getB3DApplication().getSelectedElement().getID());
        } catch (java.lang.NullPointerException npe)
        {
            selectedObjectLabel.setText("");
        }
        dimensionLabel.setText("Dimension:  " + CurrentData.getEditorWindow().getPlayPanel().getSize().width + " x " + CurrentData.getEditorWindow().getPlayPanel().getSize().height);
    }

    @Override
    public void setEnabled(boolean enabled)
    {
        for (Component component : getComponents())
        {
            component.setEnabled(enabled);
            if (component instanceof Container)
            {
                setEnabled((Container) component, enabled);
            }
        }
    }

    /**
     *
     * @param container
     * @param enabled
     */
    public void setEnabled(Container container, boolean enabled)
    {
        for (Component component : container.getComponents())
        {
            component.setEnabled(enabled);
            if (component instanceof Container)
            {
                setEnabled((Container) component, enabled);
            }
        }
    }

    private void initConfigValues()
    {
        gridCheckBox.setSelected(CurrentData.getConfiguration().getSettings().get("showgrid").equals("true"));
        filterCheckBox.setSelected(CurrentData.getConfiguration().getSettings().get("showfilters").equals("true"));
        if (CurrentData.getConfiguration().getSettings().get("camspeed").equals("10"))
        {
            camSpeedComboBox.setSelectedIndex(0);
        } else if (CurrentData.getConfiguration().getSettings().get("camspeed").equals("50"))
        {
            camSpeedComboBox.setSelectedIndex(1);
        } else if (CurrentData.getConfiguration().getSettings().get("camspeed").equals("100"))
        {
            camSpeedComboBox.setSelectedIndex(2);
        } else if (CurrentData.getConfiguration().getSettings().get("camspeed").equals("200"))
        {
            camSpeedComboBox.setSelectedIndex(3);
        } else if (CurrentData.getConfiguration().getSettings().get("camspeed").equals("400"))
        {
            camSpeedComboBox.setSelectedIndex(4);
        } else
        {
            camSpeedComboBox.setSelectedIndex(5);
        }
        wireCheckBox.setSelected(CurrentData.getConfiguration().getSettings().get("showwire").equals("true"));
        sceneryViewCheckBox.setSelected(CurrentData.getConfiguration().getSettings().get("showscenery").equals("true"));
    }

    private void initConfigListener()
    {
        physicsStartStopToggleButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (physicsStartStopToggleButton.isSelected())
                {
                    physicsStartStopToggleButton.setText("Stop Physics");
                    CurrentData.getEditorWindow().getB3DApplication().playPhysics();
                } else
                {
                    physicsStartStopToggleButton.setText("Start Physics");
                    CurrentData.getEditorWindow().getB3DApplication().pausePhysics();
                }
            }
        });
        camToCenterButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().getCamera().setLocation(Vector3f.ZERO);
            }
        });
        physicsDebugCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().getBulletAppState().setDebugEnabled(physicsDebugCheckBox.isSelected());
            }
        });
        gridCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                CurrentData.getConfiguration().getSettings().put("showgrid", Boolean.toString(gridCheckBox.isSelected()));
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        if (gridCheckBox.isSelected())
                        {
                            CurrentData.getEditorWindow().getB3DApplication().getEditorNode().attachChild(
                                    CurrentData.getEditorWindow().getB3DApplication().getGridGeometry());
                        } else
                        {
                            CurrentData.getEditorWindow().getB3DApplication().getEditorNode().detachChild(
                                    CurrentData.getEditorWindow().getB3DApplication().getGridGeometry());
                        }
                        return null;
                    }
                });
                CurrentData.getConfiguration().save();
            }
        });
        filterCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                CurrentData.getConfiguration().getSettings().put("showfilters", Boolean.toString(filterCheckBox.isSelected()));
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        if (filterCheckBox.isSelected())
                        {
                            CurrentData.getEditorWindow().getB3DApplication().getViewPort().addProcessor(
                                    CurrentData.getEditorWindow().getB3DApplication().getFilterPostProcessor());
                        } else
                        {
                            CurrentData.getEditorWindow().getB3DApplication().getViewPort().removeProcessor(
                                    CurrentData.getEditorWindow().getB3DApplication().getFilterPostProcessor());
                        }
                        return null;
                    }
                });
                CurrentData.getConfiguration().save();
            }
        });
        wireCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                CurrentData.getConfiguration().getSettings().put("showwire", Boolean.toString(wireCheckBox.isSelected()));
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        Wizard.setWireframe(CurrentData.getEditorWindow().getB3DApplication().getSceneNode(), wireCheckBox.isSelected());
                        return null;
                    }
                });
                CurrentData.getConfiguration().save();
            }
        });
        sceneryViewCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        if (sceneryViewCheckBox.isSelected())
                        {
                            CurrentData.getEditorWindow().getB3DApplication().getRootNode().detachChild(
                                    CurrentData.getEditorWindow().getB3DApplication().getEditorNode());
                        } else
                        {
                            CurrentData.getEditorWindow().getB3DApplication().getRootNode().attachChild(
                                    CurrentData.getEditorWindow().getB3DApplication().getEditorNode());
                        }
                        return null;
                    }
                });
                CurrentData.getConfiguration().getSettings().put("showscenery", Boolean.toString(sceneryViewCheckBox.isSelected()));
                CurrentData.getConfiguration().save();
            }
        });
        camSpeedComboBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (camSpeedComboBox.getSelectedIndex() == 0)
                {
                    CurrentData.getEditorWindow().getB3DApplication().getFlyByCamera().setMoveSpeed(10);
                } else if (camSpeedComboBox.getSelectedIndex() == 1)
                {
                    CurrentData.getEditorWindow().getB3DApplication().getFlyByCamera().setMoveSpeed(50);
                } else if (camSpeedComboBox.getSelectedIndex() == 2)
                {
                    CurrentData.getEditorWindow().getB3DApplication().getFlyByCamera().setMoveSpeed(100);
                } else if (camSpeedComboBox.getSelectedIndex() == 3)
                {
                    CurrentData.getEditorWindow().getB3DApplication().getFlyByCamera().setMoveSpeed(200);
                } else if (camSpeedComboBox.getSelectedIndex() == 4)
                {
                    CurrentData.getEditorWindow().getB3DApplication().getFlyByCamera().setMoveSpeed(400);
                } else
                {
                    CurrentData.getEditorWindow().getB3DApplication().getFlyByCamera().setMoveSpeed(
                            Float.parseFloat(JOptionPane.showInputDialog("New Camera Speed:")));
                }
                CurrentData.getConfiguration().getSettings().put("camspeed", Float.toString(CurrentData.getEditorWindow().getB3DApplication().getFlyByCamera().getMoveSpeed()));
                CurrentData.getConfiguration().save();
            }
        });
    }
}
